#!/usr/bin/env python
# -*- coding:utf-8 -*-

import yaml
from util.readYaml import ReadYaml
from configs import globalVariables
import os
'''
【功能】：读取Yaml文件中录入的apiName的所有接口信息
【入参】：apiName
【返回值】：apiName接口的所有信息
'''

curPath = os.path.abspath(os.path.dirname(__file__))
apifilesFolderPath = os.path.abspath(os.path.dirname(curPath) + os.path.sep + "apifiles")
# apiYamlPath = os.path.abspath(os.path.dirname(curPath) + os.path.sep + "apifiles" + os.path.sep + filename)
# print('apiYamlPath',apiYamlPath)
file_contens = {}

class GetAPIInfo(object):
    def get_fileContens(self,filename):
        global file_contens
        if filename == "" or filename is None:
            globalVariables.logger.error('请输入要读取的文件名')
        else:
            if filename in file_contens.keys():
                if len(file_contens[filename]) <= 0:
                    filePath = os.path.abspath(os.path.dirname(curPath) + os.path.sep + "apifiles" + os.path.sep + filename)
                    file_contens[filename] = ReadYaml().readYaml(filePath)
            else:
                filePath = os.path.abspath(os.path.dirname(curPath) + os.path.sep + "apifiles" + os.path.sep + filename)
                file_contens[filename] = ReadYaml().readYaml(filePath)
        return file_contens[filename]

    def getAPISendInfo(self, api_name, filename):
        if filename == '' or filename is None:
            globalVariables.logger.error('请输入要读取的文件名')
        else:
            content = self.get_fileContens(filename)
            httpSendInfo = {}
            httpSendInfo['method'] = content[api_name]['method']
            httpSendInfo['url'] = content['host']+content[api_name]['url']
            httpSendInfo['headers'] = content[api_name]['headers']
            if 'data' in list(content[api_name].keys()):
                httpSendInfo['data'] = content[api_name]['data']
            if 'files' in list(content[api_name].keys()):
                httpSendInfo['files'] = content[api_name]['files']
            return httpSendInfo

    def getAPIExpectedInfo(self, api_name,filename):
        if filename == '' or filename is None:
            globalVariables.logger.error('请输入要读取的文件名')
        else:
            content = self.get_fileContens(filename)
            httpExpectedInfo = {}
            if 'expected' in content[api_name]:
                httpExpectedInfo['expected'] = content[api_name]['expected']
            return httpExpectedInfo

if __name__ == "__main__":
    # read_yml()
    ry = GetAPIInfo()
    cont1 = ry.getAPISendInfo('getCourseInfoList','api_trainingAndExam.yml')
    cont2 = ry.getAPIExpectedInfo('getCourseInfoList','api_trainingAndExam.yml')
    print('cont1', cont1)
    print('cont2', cont2)

    ry = GetAPIInfo()
    cont1 = ry.getAPISendInfo('getCourseInfoList', 'api_trainingAndExam.yml')
    cont2 = ry.getAPIExpectedInfo('getCourseInfoList', 'api_trainingAndExam.yml')
    print('cont1', cont1)
    print('cont2', cont2)


'''
os.path.dirname(__file__)： 获取文件所在目录的完整路径；
os.path.abspath(path): 返回绝对路径
'''